package com.boot.main.service.impl;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.nativepay.NativePayService;
import com.wechat.pay.java.service.payments.nativepay.model.Amount;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayRequest;
import com.wechat.pay.java.service.payments.nativepay.model.PrepayResponse;

public class WxPayService {
    // https://github.com/wechatpay-apiv3/wechatpay-java
    public static String merchantId = "190000****"; // 商户号
    public static String privateKeyPath = "/Users/yourname/your/path/apiclient_key.pem"; // 商户API私钥路径
    public static String merchantSerialNumber = "5157F09EFDC096DE15EBE81A47057A72********"; // 商户证书序列号
    public static String apiV3Key = "...."; // 商户APIV3密钥
    public static void main(String[] args){
        Config config = new RSAAutoCertificateConfig.Builder()
                .merchantId(merchantId)
                .privateKeyFromPath(privateKeyPath)
                .merchantSerialNumber(merchantSerialNumber)
                .apiV3Key(apiV3Key)
                .build();
        NativePayService service = new NativePayService.Builder().config(config).build();
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        amount.setTotal(100);
        request.setAmount(amount);
        request.setAppid("wxa9d9651ae******");
        request.setMchid("190000****");
        request.setDescription("测试商品标题");
        request.setNotifyUrl("https://notify_url");
        request.setOutTradeNo("out_trade_no_001");
        PrepayResponse response = service.prepay(request);
        System.out.println(response.getCodeUrl());
    }
}
